﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Qreed.Xna.Font
{
    public class FilledFontCharacterMesh : VectorFontCharacterMesh
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FilledFontCharacter"/> class.
        /// </summary>
        /// <param name="font">The font.</param>
        internal FilledFontCharacterMesh(RenderFont font)
            : base(font)
        {
            
        }

        /// <summary>
        /// Creates a flat character.
        /// </summary>
        /// <param name="basedOnCharacter">The based on character.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="indices">The indices.</param>
        internal static void CreateFlatCharacter(VectorFontCharacter basedOnCharacter, out VertexPositionNormalTexture[] vertices, out short[] indices)
        {
            List<VertexPositionNormalTexture> vertexList = new List<VertexPositionNormalTexture>();
            List<short> indexList = new List<short>();

            for (int j = 0; j < basedOnCharacter.Vertices.Length; j++)
            {
                VertexPositionNormalTexture vertex = new VertexPositionNormalTexture();

                vertex.Position = new Vector3(basedOnCharacter.Vertices[j], 0.0f);
                vertex.Normal = Vector3.Backward;

                vertexList.Add(vertex);
            }

            foreach (VectorFontCharacterTriangle triangle in basedOnCharacter.Triangles)
                foreach (short index in triangle.Indices)
                    indexList.Add(index);

            vertices = vertexList.ToArray();
            indices = indexList.ToArray();
        }

        /// <summary>
        /// Builds the vertex buffer.
        /// </summary>
        /// <param name="copyFrom">The copy from.</param>
        /// <param name="vertices">The vertices.</param>
        /// <param name="indices">The indices.</param>
        protected override void BuildVertexBuffer(VectorFontCharacter copyFrom, out VertexPositionNormalTexture[] vertices, out short[] indices)
        {
            CreateFlatCharacter(copyFrom, out vertices, out indices);
        }
    }
}
